﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace GameName1
{
    class Player : GameObject
    {
        const float DEFAULT_HEALTH = 100;
        const float DEFAULT_ARMOR = 0;

        //PLAYERS SPECIFIC
        private bool isJumping = false;
        private bool isFalling = false;
        private float JumpHeight = -100.0f;
        private float JumpedH = 0.0f;
        private float JumpVelocity = 250.0f;
        private float curJumpVelocity = 0.0f;
        private float Gravity = 13.0f;

        public Player(Vector2 i_Pos) : base(i_Pos)
        {
            m_armor = DEFAULT_ARMOR;
            m_facing = true;
            m_health = DEFAULT_HEALTH;

            m_offsetL = new Vector2(8, 32);
            m_offsetR = new Vector2(40, 32);
            m_width = 32;
            m_height = 50;
            m_bbox = new Rectangle((int)m_Pos.X +16, (int)m_Pos.Y+14, (int)m_width, (int)m_height);
        }

        public bool Jumping
        {
            get { return isJumping; }
            private set { isJumping = value; }
        }

        public bool Falling
        {
            get { return isFalling; }
            private set { isFalling = value; }
        }

        public void ActionJump()
        {
            if (Jumping || Falling )
                return;

            Jumping = true;
            JumpedH = -JumpHeight;
            curJumpVelocity = JumpVelocity;
        }
        public override void Draw(ref Microsoft.Xna.Framework.Graphics.SpriteBatch sbatch)
        {
                //sbatch.Draw(m_texhandler.Texture, m_Pos, TexHandler.TexColor);
            Rectangle d = new Rectangle((int)m_Pos.X,(int)m_Pos.Y,m_texhandler.Width,m_texhandler.Height);
            sbatch.Draw(TexHandler.Texture, d, m_texhandler.SourceRect, TexHandler.TexColor);
            DrawColBox(ref sbatch);

        }

        public override void Update(double t)
        {
            //JUMPING
            if (Jumping)
            {
                
                float d = (float)t*curJumpVelocity;
                Move(0, -d);

                JumpedH -= d;

                if (JumpedH <= 0.0f)
                {
                    Jumping = false;
                    Falling = true;
                    curJumpVelocity = 0.0f;
                }
            }
            else if(m_Pos.Y<300 && Falling)  //FALLING ASSUMMING 300 IS FLOOR
            {
                float d = (float)t * Gravity;
                curJumpVelocity += d;
                Move(0, curJumpVelocity);

            }
            else
            {
                Falling = false;
            }

            //MOVESTATE
            if (!HasMoved && m_facing)
            {
                m_texhandler.MoveState = TextureHandler.MOVE_STATE.STANDINGR;
            }
            else if (!HasMoved && !m_facing)
            {
                m_texhandler.MoveState = TextureHandler.MOVE_STATE.STANDINGL;
            }


            base.Update(t);
        }

    }
}
